package org.neuedu.vblogserver.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.neuedu.vblogserver.bean.RespBean;
import org.neuedu.vblogserver.bean.Role;
import org.neuedu.vblogserver.bean.User;
import org.neuedu.vblogserver.bean.UserBean;
import org.neuedu.vblogserver.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
       User user = userMapper.loaderUserByUsername(s);

       if (user==null){
           throw new UsernameNotFoundException("用户名不存在");
       }
        List<Role> roles =userMapper.getRolesById(user.getId());
        user.setRoles(roles);
       return user;
    }


    public PageInfo getUsers(String serchKeyWord, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserBean> users =userMapper.getUsers(serchKeyWord);
        PageInfo pageInfo = new PageInfo(users);
        return pageInfo;

    }

    /*public RespBean delUserRole(String uid, String rid) {


    }*/
}
